<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="参数编码" :label-width="formLabelWidth" prop="paramCode">
				<el-input v-model="form.paramCode" autocomplete="off" placeholder="请输入参数编码"></el-input>
			</el-form-item>
			<el-form-item label="参数值" :label-width="formLabelWidth" prop="paramValue">
				<el-input v-model="form.paramValue" autocomplete="off" placeholder="请输入参数值"></el-input>
			</el-form-item>
			<el-form-item label="类型" :label-width="formLabelWidth" prop="paramType">
				<fortress-select v-model="form.paramType" :dictList="paramTypeList" placeholder="请选择类型" clearable></fortress-select>
			</el-form-item>
			<el-form-item label="参数的描述" :label-width="formLabelWidth" prop="paramRemark">
				<el-input type="textarea" :rows="2" placeholder="请输入参数的描述" v-model="form.paramRemark"></el-input>
			</el-form-item>
			<el-form-item label="状态" :label-width="formLabelWidth" prop="status">
				<el-switch
					v-model="form.status"
					active-color="#13ce66"
					inactive-color="#ff4949"
					:active-value="1"
					:inactive-value="2"
					active-text="启用"
					inactive-text="禁用"
				></el-switch>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { configparamsInfo, configparamsSave, configparamsEdit } from "@/api/system/configparams.js"
import { enumList } from "@/api/public.api.js"
import * as ftVal from "@/common/fortressValidator.js"
export default {
	data() {
		return {
			visible: false,
			form: {
				paramCode: "",
				paramValue: "",
				paramType: "",
				paramRemark: "",
				status: "2",
			},
			formLabelWidth: "120px",
			rules: {
				paramCode: [{ validator: ftVal.validateAlphabets, trigger: "blur" }],
			},
			YesNoList: [],
			paramTypeList: [],
		}
	},
	created() {},
	methods: {
		queryEnumList() {
			this.YesNoList = []
			enumList("YesNo", "").then(resp => {
				if (resp.code == 0) {
					this.YesNoList = resp.data
				}
			})
		},
		queryJsonList() {
			this.paramTypeList = []
			let paramTypeListData = '[{"code":"1","name":"系统参数"},{"code":"2","name":"业务参数"}]'
			this.paramTypeList = JSON.parse(paramTypeListData)
		},
		init(id) {
			this.resetForm()
			this.queryEnumList()
			this.queryJsonList()
			//修改
			if (id) {
				configparamsInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
							}
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			}
			this.visible = true
		},
		//提交表单数据
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					//修改
					if (this.form.id) {
						configparamsEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						configparamsSave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				paramCode: "",
				paramValue: "",
				paramType: "",
				paramRemark: "",
				status: "2",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
